package by.dezhits.liltweets.images;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;

public class ImageProccessor {
	private ImageProccessor() {};
	
	public static Bitmap getRoundedCornersImage(Bitmap source, int radiusPixels) {
		
		if (source == null) {
			return null;
		}
		final int sourceWidth = source.getWidth();
		final int sourceHeight = source.getHeight();
		final Bitmap output = Bitmap.createBitmap(sourceWidth, sourceHeight, Bitmap.Config.ARGB_8888);
		final Canvas canvas = new Canvas(output);
		
		final int color = 0xFF000000;
		final Paint paint = new Paint();
		paint.setColor(color);
		
		final Rect rect = new Rect(0, 0, sourceWidth, sourceHeight);
		final RectF rectF = new RectF(rect);
		
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		canvas.drawRoundRect(rectF, radiusPixels, radiusPixels, paint);
		
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(source, 0, 0, paint);
		
		source.recycle();

		
		return output;
	}
}
